#include "cfg.hpp"


CFGedge::CFGedge(CFGvertex *_src) : src(_src), dst(NULL) {}
CFGvertex* CFGedge::getSource() const {
	return this->src;
}
CFGvertex* CFGedge::getDestination() const {
	return this->dst;
}
void CFGedge::setDestination(CFGvertex *dest) {
	if (this->dst != NULL) {
		assert(!this->dst->incoming.empty());
		this->dst->incoming.erase(this);	// remove from incoming edges from the old vertex
	}
	this->dst = dest; 
	if (this->dst != NULL) {
		this->dst->incoming.insert(this);	// add to incoming edges on the new vertex
	}
}

CFGvisitable::CFGvisitable() : visited(0) {}

CFGvertex::incoming_t CFGvertex::incoming_edges() const {
	return this->incoming;
}

int CFGvertex::label() const {
	return this->this_number();
}

box::box(): child(this), payload(NULL) {}
void box::acceptVisit(CFGvisitor *visitor) {visitor->visit(this);}
outgoing_t box::outgoing_edges() const {
	outgoing_t ret;
	if (this->child.getDestination()) ret.push_back(&(this->child)); // add only edge if it points to something
	return ret;
}


diamond::diamond() : true_branch(this), false_branch(this), condition(NULL) {}
void diamond::acceptVisit(CFGvisitor *visitor) {visitor->visit(this);}
outgoing_t diamond::outgoing_edges() const {
	outgoing_t ret;
	if (this->true_branch.getDestination()) ret.push_back(&(this->true_branch)); // add only edge if it points to something
	if (this->false_branch.getDestination()) ret.push_back(&(this->false_branch)); // add only edge if it points to something
	return ret;
}
